<template>
  <div class="container">
    <div style="width: 400px; padding: 30px; background-color: white; border-radius: 5px;">
      <div style="text-align: center; font-size: 20px; margin-bottom: 20px; color: #333">欢迎注册</div>
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" placeholder="请输入账号" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" placeholder="请输入密码" show-password  v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item prop="confirmPass">
          <el-input prefix-icon="el-icon-lock" placeholder="请确认密码" show-password  v-model="form.confirmPass"></el-input>
        </el-form-item>

        <el-form-item prop="email" >
          <el-input prefix-icon="el-icon-lock" placeholder="请输入邮箱"  v-model="form.email" ></el-input>
          <!--          <el-button class="btn-orange" style="margin-right: -2px" :plain="true" :disabled="disabled"
                               @click="getcode">{{ valiBtn }}
                    </el-button>-->
        </el-form-item>
        <el-form-item prop="code"  >
          <el-row :gutter="6">
            <el-col :span="17">
              <el-input placeholder="请输入验证码" v-model="form.code">
                <i slot="prefix" class="el-icon-tickets"></i>
              </el-input>
            </el-col>
            <el-col :span="6">
              <el-button @click="getcode" :disabled="disabled">{{valiBtn}}</el-button>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item>
          <el-select v-model="form.role" placeholder="请选择角色" style="width: 100%">
            <el-option label="商家" value="BUSINESS"></el-option>
            <el-option label="用户" value="USER"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%; background-color: #333; border-color: #333; color: white" @click="register">注 册</el-button>
        </el-form-item>
        <div style="display: flex; align-items: center">
          <div style="flex: 1"></div>
          <div style="flex: 1; text-align: right">
            已有账号？请 <a @click="$router.push('/login')" style="cursor: pointer">登录</a>
          </div>
        </div>
      </el-form>
    </div>
  </div>
</template>



<script>

export default {
  name: "Register",
  data() {
    // 验证码校验
    const validatePassword = (rule, confirmPass, callback) => {
      if (confirmPass === '') {
        callback(new Error('请确认密码'))
      } else if (confirmPass !== this.form.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      form: {},
      disabled: false,
      valiBtn: '获取验证码',
      email: '',
      code:"",
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        confirmPass: [
          { validator: validatePassword, trigger: 'blur' }
        ],
        email: [
          {required: true, message: '请输入邮箱地址', trigger: 'blur'},
          {type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change']}
        ]

      }
    }
  },
  created() {

  },
  methods: {
    register() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          // 验证通过
          this.$request.post('/register', {
            username:this.form.username,
            password:this.form.password,
            role:this.form.role,
            email:this.form.email,
            code:this.form.code
          }).then(res => {
            console.log(res)
            if (res.code === 0) {
              this.$router.push('/')  // 跳转登录页面
              this.$message.success('注册成功')
            } else {
              this.$message.error(res.message)
            }
          })
        }
      })
    },
    getcode() {
      let time = 60;
      const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
      console.log(emailRegex.test(this.form.email))
      if (this.form.email === "" || emailRegex.test(this.form.email) === false) {
        this.$message('请输入正确的邮箱');
      } else {

        this.$request.get(`/sendEmail/${this.form.email}`, {}).then(res => {
          console.log(res)
          if (res.code === 0) {
            this.$message.success('发送成功')
          } else {
            this.$message.error(res.message)
          }
        })
      }
      let timer = setInterval(() => {
        if (time === 0) {
          clearInterval(timer);
          this.valiBtn = '获取验证码';
          this.disabled = false;
        } else {
          this.disabled = true;
          this.valiBtn = time + '秒后重试';
          time--;
        }
      }, 1000);
    }


    },


}
</script>

<style scoped>
.container {
  height: 100vh;
  overflow: hidden;
  background-image: url("@/assets/imgs/bg.jpg");
  background-size: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #666;
}
a {
  color: #2a60c9;
}
</style>
